<template>
  <div />
</template>
<script>
import { sosWay, tokenConfig } from '@/api/user'
import { setTokenConfig, getTokenConfig } from '@/util/auth'
import { redirectLogin } from '@/util/redirect'

export default {
  name: 'TokenConfig',
  data() { return {} },
  mounted() {
    if (process.env.VUE_APP_TYPE === 'boot' || this.$route.query.isAuth === false) {
      this.init()
    } else {
      this.sosWay()
    }
  },
  methods: {
    checkNeedLogin() {
      if (this.$route.query.isAuth === false) {
        this.$router.replace(this.$route.query.path)
        return
      }
      redirectLogin()
    },
    init() {
      if (getTokenConfig()) {
        this.checkNeedLogin()
        return
      }
      tokenConfig().then(config => {
        setTokenConfig(config.data.data)
        this.checkNeedLogin()
      })
    },
    sosWay() {
      sosWay().then(() => {
        this.$router.replace('/')
      }).finally(() => {
        this.init()
      })
    }
  }
}
</script>
